<?php
/**
 * Created by PhpStorm.
 * User: moonlight
 * Date: 2015/6/3
 * Time: 14:47
 */

namespace Home\Model;


use Think\Model;

class BlogModel extends Model{
    protected $msg = array(
        'status'    =>  0,
        'blog_id'   =>  null,
        'info'      =>  '',
    );
    //发布blog
    public function publishBlog($data){
        $user_id = session('user_id');
        if(!$user_id){      //id为空，用户未登录，或已过期，必须重新登录
            $this->msg = array(
                'status'    =>  0,
                'blog_id'   =>  null,
                'info'      =>  '请重新登录',
            );
            return $this->msg;
        }
        $BlogData = array(
            'user_id'   =>  $user_id,
            'area_id'   =>  $data['area_id'],
            'blog_title'    =>  $data['blog_title'],
            'blog_content'  =>  $data['blog_content'],
        );
        $BlogData['blog_date'] = time();

        $result = $this->data($BlogData)->add();
        if(!$result){//增加失败
            $this->msg = array(
                'status'    =>  0,
                'blog_id'   =>  null,
                'info'      =>  '发布失败（001）',   //001表示增加blog时失败
            );
            return $this->msg;
        }
        $this->msg  =   array(
            'status'    =>  1,
            'blog_id'   =>  $result,
            'info'      =>  'success!',
        );
        return  $this->msg;

    }
    /**
     * 获取论坛列表
     * @param $area_id 论坛区域id
     */
    public function getBlogList($area_id,$isPage = 1){
        $condition['area_id']   =   $area_id;
        if(0 === $isPage){//不分页
            return $this->where($condition)->order('blog_date desc')->select();
        }
        $page = I('p',1,'int');
        $blog_list = $this->where($condition)->order('blog_date desc')->page($page,13)->select();
        $count = $this->count();//获取总条数
        $Page = new\Think\Page($count,13);
        $show = $Page->show();
        return array("lists" => $blog_list, "page" => $show);
    }
    public function addBlogInfo($data){
        $condition['blog_id']   =   $data['blog_id'];
        $result =   $this->where($condition)->find();
        if($result){
            $result['energy_blog_picture'] = $data['energy_blog_picture'];
            return $this->where($condition)->save($result);
        }
    }
    /**
     * 获取blog信息 并返回
     * @param $blog_id
     * @return 返回获得的帖子相关信息=>blog表信息
     */
    //关联模型 一对一关系 HAS_ONE  每一个博客属于一个user，对应一个good，拥有0-n个回复者，关注者
    //在thinkphp论坛看到关联模型和视图模型并不完善，那现在就不尝试使用那些了  自己来写，一个个查。
    public function getBlogData($blog_id){
        if(empty($blog_id)){
            return 'blog_id为空';
        }else{
            $condition['blog_id']   =   $blog_id;
            $blogData   =   $this->where($condition)->find();
            if(!$blogData){
                return  false;
            }
            //if($blogData[''])
            return  $blogData;
        }
        return;
    }

    /**
     * 添加回复
     * @param $replyData
     * @return array()
     */
    public function addReply($replyData){
        $Reply =    M('Reply');
        $msg    =   array(
            'status'    =>  0,
            'info'      =>  '回复失败',
        );
        $data   =   array(
            'blog_id'   =>   $replyData['blog_id'],
            'user_id'   =>  session('user_id'),
            'reply_type'    =>  $replyData['reply_type'],
            'reply_content' =>  $replyData['reply_content'],
            'reply_time'    =>  time(),
        );
        if($replyData['reply_type'] == 1){
            $data['rereply_id'] =   $replyData['rereply_id'];
        }
        $result =   $Reply->data($data)->add();
        if($result){
            $msg    =   array(
                'status'    =>  1,
                'info'      =>  '回复成功',
            );
            return array('result'=>$result,'msg'=>$msg);
        }else{
            return array('result'=>$result,'msg'=>$msg);
        }
    }

    /**
     * 添加回复数，最近回复
     */
    public function addReplyInfo($data){
        $condition['blog_id']   =   $data['blog_id'];
        $result =   $this->where($condition)->find();
        if($result){
            $result['replies_num'] ++;
            $result['last_reply_user']  =   session('username');
            return $this->where($condition)->save($result);
        }
    }

    /**
     * 获得回复信息
     * @param $blog_id
     * @return array()
     */
    public function getReplies($blog_id){
        $condition['blog_id']   =   $blog_id;
        $condition['reply_type']    =   0;
        $page   =   I('p',1,'int');
        $Reply  =   M('reply');
        $reply_list =   $Reply->where($condition)->order('reply_time asc')->page($page,5)->select();
        $count  =   $Reply->where($condition)->count();
        $Page = new\Think\Page($count,8);
        $show   =   $Page->show();
        return array('reply_list'=>$reply_list,"page"=>$show);
    }

    public function blogSearch($searchData){
        //dump($searchData['search_content']);
        $map['blog_title|blog_content'] =   array('like','%'.$searchData["search_content"].'%');
        $result1 = $this->where($map)->select();
        //$map['goods_name']
        //dump($result);
        if($result1){
            return array('blog'=>$result1);
        }
    }
}